package com.cqs.learning.spring.cloud.app.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * ribbon + restTemplate实现客户端负责
 *
 * @author lixw
 * @date created in 9:38 2020/4/9
 */
@Configuration
public class RestTemplateConfig {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * 修改负载均衡的算法 默认轮询
     * <p>
     * Spring cloud 使用得是 BlockingLoadBalancerClient 目前只支持轮询(RoundRobinLoadBalancer)
     * <p>
     * 自动配置类BlockingLoadBalancerClientAutoConfiguration
     * <p>
     * //TODO 目前不知道如何指定 方案为 RibbonLoadBalancerClient
     * <p>
     * <p>
     * 不生效
     *
     * @return
     */
    @Bean
    public IRule rule() {
        return new WeightedResponseTimeRule();
    }

}

